package com.momoback.feign;

import com.alibaba.fastjson.JSON;
import feign.RequestTemplate;
import feign.codec.Encoder;

import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;

public class FastjsonEncoder implements Encoder {


    @Override
    public void encode(Object object, Type bodyType, RequestTemplate template) {
        // 检查对象是否为 JSONObject 类型
        if (object instanceof com.alibaba.fastjson.JSONObject) {
            // 将 JSONObject 转换为 JSON 字符串
            byte[] bytes = JSON.toJSONString(object).getBytes(StandardCharsets.UTF_8);
            // 设置请求体和字符集
            template.body(bytes, StandardCharsets.UTF_8);
            template.header("Content-Type", "application/json; charset=utf-8");
        }
    }
}